package it.truestoryfactory.tsq.game.db;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "MyDatabase";
	private static final int DATABASE_VERSION = 1;
	@SuppressLint("SdCardPath")
	private static final String DATABASE_DIR = "/data/data/it.truestoryfactory.tsq/databases/";
	private static final String DATABASE_PATH = DATABASE_DIR+DATABASE_NAME;
	//private static final String DATABASE_OLD_PATH = DATABASE_DIR+"old_"+DATABASE_NAME;

	private Context myContext;
	private SQLiteDatabase myDB;

	public DataBaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.myContext=context;
	}
	
	public void createDB() throws IOException {
		if (!checkDB()) {
			Log.d(this.getClass().getName(), "Not check");
			this.getReadableDatabase();
			copyDB();
		}	
	}

	private boolean checkDB() {
		SQLiteDatabase checkDB=null;
		Log.d(this.getClass().getName(), "call check DB");
		try {
			checkDB= SQLiteDatabase.openDatabase(DATABASE_PATH, null, SQLiteDatabase.OPEN_READONLY);
		
		}catch(SQLiteException e){
		 
		//database does't exist yet.
		 
		}
	
		Log.d(this.getClass().getName(), "DB check");
		if (checkDB != null) {
			checkDB.close();
			return true;
		}
		return false;
	}

	private void copyDB() throws IOException  {
		
		InputStream is = myContext.getAssets().open(DATABASE_NAME);
		OutputStream os = new FileOutputStream(DATABASE_PATH);
		
		byte[] buffer = new byte[1024];
		int lenght;
		while ((lenght = is.read(buffer)) > 0 )
			os.write(buffer, 0, lenght);
		
		os.flush();
		os.close();
		is.close();
		
	}
	
	public void openDB() throws SQLiteException{
		myDB = SQLiteDatabase.openDatabase(DATABASE_PATH, null, SQLiteDatabase.OPEN_READONLY);
		
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.d(this.getClass().getName(), "Helper on Create DB");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.d(this.getClass().getName(), "Helper on Upgrade DB");

	}
	
	@Override
	public synchronized void close() {
		if (myDB != null)
			myDB.close();
		super.close();
	}
	
	public TSQuestion getQuestion(String topicCode, int quest){
		TSQuestion q = new TSQuestion();
		
		Cursor c = myDB.query("QuestionStrings", new String[]{"question", "correct_answer"}, "topic_id=\""+topicCode+"\" AND quest_id=" + quest, null, null, null, null);
		c.moveToFirst();
		q.setQuestionString(c.getString(0));
		q.setCorrectAnswerIndex(c.getInt(1)-1);
		
		c = myDB.query("AnswerStrings", new String[]{"answ_id", "answer"}, "topic_id=\""+topicCode+"\" AND quest_id=" + quest, null, null, null, null);
		while(c.moveToNext())
			q.setAnswer(c.getInt(0)-1,c.getString(1));
				
		q.printQuestion();
		return q;
	}
}
